import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { getDiscountData, getGoodPriceData, getHighScoreData, getHotRecommendData, getLongForData, getPlusData } from '@/servers/modules/home'

export const fetchHomeData = createAsyncThunk('fetchHomeData', (payload, { dispatch, getState }) => {
	getGoodPriceData().then(res => {
		dispatch(changeGoodPriceDataAction(res))
	})
	getHighScoreData().then(res => {
		dispatch(changeHighScoreDataAction(res))
	})
	getDiscountData().then(res => {
		dispatch(changeDiscountDataAction(res))
	})
	getHotRecommendData().then(res => {
		dispatch(changeHotRecommendDataAction(res))
	})
	getLongForData().then(res => {
		dispatch(changeLongForDataAction(res))
	})
	getPlusData().then(res => {
		dispatch(changePlusDataAction(res))
	})
})

const homeSlice = createSlice({
	name: 'home',
	initialState: {
		goodPriceData: {},
		highScoreData: {},
		discountData: {},
		hotRecommendData: {},
		longForData: {},
		plusData: {}
	},
	reducers: {
		changeGoodPriceDataAction(state, { payload }) {
			state.goodPriceData = payload
		},
		changeHighScoreDataAction(state, { payload }) {
			state.highScoreData = payload
		},
		changeDiscountDataAction(state, { payload }) {
			state.discountData = payload
		},
		changeHotRecommendDataAction(state, { payload }) {
			state.hotRecommendData = payload
		},
		changeLongForDataAction(state, { payload }) {
			state.longForData = payload
		},
		changePlusDataAction(state, { payload }) {
			state.plusData = payload
		}
	}
	// extraReducers: {
	// 	[fetchHomeData.fulfilled](state, { payload }) {
	// 		state.goodPriceData = payload
	// 	}
	// }
})

export const { changeGoodPriceDataAction, changeHighScoreDataAction, changeDiscountDataAction, changeHotRecommendDataAction, changeLongForDataAction, changePlusDataAction } = homeSlice.actions
export default homeSlice.reducer
